﻿#include <vector>
#include <thread>
#include <unistd.h>
#include "loop_controller.h"
#include "tcp_client.h"
static void sig_deal(int signo);
std::vector<tcp_client*> vclient;
#define TCP_CLIENT 1
#if TCP_CLIENT
int main(int argc, char** argv)
{
	if (signal(SIGINT, sig_deal) == SIG_ERR)
		printf("can't catch SIGINT\n");
	if (signal(SIGTERM, sig_deal) == SIG_ERR)
		printf("can't catch SIGTERM\n");
	if (signal(SIGSEGV, sig_deal) == SIG_ERR)	
		printf("can't catch SIGSEGV\n");	
	for (int i=0; i<20000;i++)
	{
		tcp_client* client=new tcp_client;
		vclient.push_back(client);		
		client->on_conn_status_cb([](tcp_connect* client, e_io_evt evt){
			if (e_tcp_conn_suc == evt)
			{
				printf("tcp connect suc\n");
			}
			else
			{
				printf("tcp connect falied\n");
			}
		});
		client->on_bytes_call([](tcp_connect* client, char* buf, size_t bsize){
			printf("%s\n", buf);
			client->write_bytes(buf, bsize);
		});

		client->on_close([](tcp_connect* tcpconn){

		}
		);
		client->set_reconn_interal(0);		
		client->connect("192.168.1.9", 8011);		
		usleep(1000);
	}			
	g_loop_contor.start_run();
	for (auto& itr : vclient)
	{
		delete itr;
	}		
	return 0;
}
#endif
static void sig_deal(int signo)
{
	switch (signo)
	{	
	case SIGINT:
		printf("catch SIGINT, Process Exit\n");
		for (auto& itr:vclient)
		{
			itr->close();
		}
		g_loop_contor.stop_run();
		break;
	case SIGTERM:
		printf("catch SIGTERM, Process Exit\n");				
		g_loop_contor.stop_run(); 
		exit(0);
		break;
	case SIGSEGV:
		break;
	default:
		printf("catch SIG %d, Process Maybe core\n", signo);		
		signal(signo, SIG_DFL);
		break;
	}
}

